<?php
class GroupeManager
{
    /**
     * Variable de " globale"
     * */
    protected $db;                          // Liaison base de donnee
    protected $lang;                        // Fichier lanh
    protected $session;                     // Class session
    protected $setting;                     // Tableau des setting
    protected $smarty;                      // Class smarty pour affichage
    protected $cache;                       // Class lite pour acceder au cache
    protected $groupe;                      // Object class Groupe
    
    public $_groupe;  
    private $_error = '';  
    private $_printMessage = true;                    
    
    /**
     * @desc Construit la class
     * */
    public function __construct($paramClass)
    {
        if(is_array($paramClass))
        {
            foreach($paramClass as $key => $value)
            {
                $this->$key = $value;
            } 
        }     
    } 
    
    /**
     * @desc Affiche les message d action
     * @param $text : message a afficheer
     * @param $type : definie l encadrement et la couleur
     * @return void
     * */
    public function printErrorMessage($text, $type = 'success')
    {
        if($this->_printMessage === true)
        {
            $this->smarty->assign('error_msg', true);
            $this->smarty->assign('error_class', 'error_' . $type);
            $this->smarty->assign('error_image', 'comment_' . $type);
            $this->smarty->assign('error_message',$text);
            $this->smarty->assign('url_site',$this->setting['url_site']);   
        }        
    }  
    
    public function setAdd()
    {
        $this->session->csrf_check(true);
        
        if(!$nom = retrieve('POST','nom',true))
            $this->_error .= 'No name group <br />';
        
        $description = retrieve('POST','description');
        
        if(! $this->checkNameExist($nom) )
            $this->_error .= 'Group name aleary use<br />';
        
   
        if($this->_error == '')
        {
            $QueryArray = array(
            'nom'   =>  addslashes(trim($nom)),
            'description'   =>  addslashes($description),
            'poid'  =>  0,
            'images'    =>  '',
            'droit_sup' =>  '0'); 
            
            $this->db->sql_query(CreateQueryInsert($QueryArray, 'immo_groupe')); 
            
            $this->printErrorMessage($this->lang['Groupe_ajoute']); 
        }
        else
            $this->printErrorMessage($this->lang['Groupe_non_ajoute'] . '<br />' . $this->_error, 'warning');
    }  
    
    public function setEdit()
    {
        $this->session->csrf_check(true);   
        
        if( !$groupe_id = retrieve('POST', 'groupe_id', true))
            $this->_error .= 'No group select<br />';
        
        if(!$nom = retrieve('POST','nom',true))
            $this->_error .= 'No name group <br />';
        
        $description = retrieve('POST','description');
        
        if(! $this->checkNameExist($nom) )
            $this->_error .= 'Group name aleary use<br />';
            
        if( $this->_error == '')
        {
            $QueryArray = array (
            'nom'   =>  addslashes(trim($nom)),
            'description'   =>  addslashes($description));
            
            $this->db->sql_query(CreateQueryUpdate($QueryArray, 'immo_groupe', " id = '". $groupe_id ."'"));
            
            $this->printErrorMessage($this->lang['Groupe_modifie']);
        }
        else
            $this->printErrorMessage($this->lang['Groupe_non_modifie'] . '<br />' . $this->_error, 'warning');
        
    }
    
    public function setDelete()
    {
        $this->session->csrf_check_get(true);
        
        if(!$groupe_id = retrieve('GET','groupe_id',true)) redirect('groupe.php?error=no_groupe_select');
        
        $this->db->sql_query("DELETE FROM immo_groupe WHERE id = '". $groupe_id ."'");
        $this->db->sql_query("DELETE FROM immo_groupe_user_link WHERE gid = '". $groupe_id ."'");
        
        $this->printErrorMessage($this->lang['Groupe_supprime']);   
    }
    
    private function checkNameExist($nom)
    {
        $this->db->sql_query("SELECT id FROM immo_groupe WHERE nom = '" . addslashes(trim($nom)) . "'");
        
        if($this->db->nb_resultat == 0)
            return true;
        else
            return false;
    }
}
?>